package com.tian.service;

import com.tian.dto.UserApplyListDetailRespDto;
import com.tian.dto.UserApplyListReqDto;
import com.tian.dto.UserApplyListRespDto;
import com.tian.dto.UserApplyRespDto;
import com.tian.entity.UserApply;
import com.tian.enums.FlagEnum;
import com.tian.enums.ResultCode;
import com.tian.enums.UserApplyStatusEnum;
import com.tian.mapper.UserApplyMapper;
import com.tian.utils.CommonResult;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年09月18日 10:40
 * 在线刷题 1200+题和1000+篇干货文章：<a href="https://woaijava.cc/">博客地址</a>
 * <p>
 * 用户贷款申请 以及  用户可用额度
 */
@Service(version = "1.0.0")
public class UserApplyDubboServiceImpl implements UserApplyDubboService {

    @Resource
    private UserApplyMapper userApplyMapper;

    @Resource

    @Override
    public CommonResult apply(Long mainId) {
        UserApply userApply = new UserApply();
        userApply.setCreateTime(new Date());
        userApply.setLoanLimit(BigDecimal.ZERO);
        userApply.setStatus(UserApplyStatusEnum.INIT.getStatus());
        userApply.setUseLoanLimit(BigDecimal.ZERO);
        int flag = userApplyMapper.insert(userApply);
        if (flag == FlagEnum.SUCCESS.getFlag()) {
            return CommonResult.success();
        }
        return CommonResult.failed(ResultCode.FAILED);
    }

    @Override
    public CommonResult<UserApplyRespDto> getByMainId(Long mainId) {
        UserApply userApply = userApplyMapper.selectByMianId(mainId);
        if (userApply == null) {
            return CommonResult.failed(ResultCode.PARAMETER_ERROR);
        }
        // TODO: 2023/9/18 参数转换
        UserApplyRespDto userApplyRespDto = new UserApplyRespDto();
        return CommonResult.success(userApplyRespDto);
    }

    @Override
    public CommonResult approve(Long userApplyId, Integer status, BigDecimal limit) {
        UserApply record = new UserApply();
        //审批通过，把额度设置给用户
        if (UserApplyStatusEnum.SUCCESS.getStatus() == status) {
            record.setLoanLimit(limit);
        }
        record.setId(userApplyId);
        record.setStatus(status);
        userApplyMapper.approve(record);
        return CommonResult.success();
    }

    @Override
    public CommonResult<UserApplyListRespDto> list(UserApplyListReqDto userApplyListReqDto) {
        List<UserApply> userApplyList = userApplyMapper.list(userApplyListReqDto.getStatus(), userApplyListReqDto.getPage(), userApplyListReqDto.getPageSize());
        UserApplyListRespDto userApplyListRespDto = new UserApplyListRespDto();
        List<UserApplyListDetailRespDto> dataList = new ArrayList<>();

        // TODO: 2023/9/18 userApplyList转dataList

        userApplyListRespDto.setDataList(dataList);

        return CommonResult.success(userApplyListRespDto);
    }
}
